<template>
  <div>
    <!-- table -->
    <BreakSpace label="旧件销毁申请" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel" :list="formList" :cols="4" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <BreakSpace label="上传视频/压缩文件" :containerStyle="{ marginBottom: '10px' }" />
    <Spin :spinning="loading" tip="Loading...">
      <FilterTable
        ref="fileTable"
        :columns="columnsFile"
        columnsRef="whbydUploadFile"
        :isSelectColumn="false"
        :onColumnsChange="columnsFile => (this.columnsFile = columnsFile)"
        height=auto
        :dataSource="list"
      />
    </Spin>
    <BreakSpace label="旧件列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onCellChange="onCellChange"
      :isSelectColumn="false"
      height="auto"
      :isPagination=false
    >
<!--      <template slot="controls" slot-scope="props">-->
<!--&lt;!&ndash;        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">添加</multiuse-button>&ndash;&gt;-->
<!--      </template>-->
    </FilterTable>
    <div style="padding-top: 3rem"></div>
    <BreakSpace label="操作记录" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table1"
      :columns="columns1"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="(columns) => (this.columns1 = columns)"
      height="auto"
      :isPagination=false
    > </FilterTable>
    <div style="padding-top: 3rem"></div>
    <!-- <FormPanel ref="formPanel1" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandleFile" /> -->
    <!-- <div style="padding-top: 3rem"></div> -->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
<!--      <multiuse-button type="primary" @click="apply">申请</multiuse-button>-->
<!--      <multiuse-button type="primary" @click="saveDialog">保存</multiuse-button>-->
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visibleList" :title="'旧件列表'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <usedPartsList @close="closeAdd" @save="saveAdd"></usedPartsList>
    </BaseDialog>
  </div>
</template>
<script>
import usedPartsList from './usedPartsList.vue';
import { applyDestory } from '@/api/usedPartsDestructionDealer/usedPartsDestroyedApplyQuery';
import { getPartType } from '@/api/dictionary';

export default {
  components: {
    //   Vehicle,
    usedPartsList,
  },
  props: ['editRow'],
  // mixins: [dictionary],
  data() {
    return {
      dataList: [],
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      columns: this.creatHeader(),
      columns1: this.creatHeader1(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      columnsFile: this.createdTable(),
      saveData: null,
      activeName: '审核记录',
      saveDataFile: {},
      visibleList: false,
      list: [],
    };
  },
  created() {
    this.assignment();
  },
  mounted() {
    let list = Object.assign([], this.editRow.dtlList);
    list.map(x => {
      x.claimNo = x.cmsClaimNo;
      x.pestructionCount = x.partCount;
      x.partCount = x.claimPartCount;
      x.claimId = x.cmsClaimId;
    });
    this.$refs.table.$refs.pageTable.EXECUTE_INSERT(list);
    this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(this.editRow.logList);
    this.$refs.fileTable.$refs.pageTable.EXECUTE_INSERT(this.editRow.fileList);
  },
  methods: {
    async assignment() {
      let res = await getPartType({ type: 5531 });
      for (let row in this.editRow) {
        this.formList.map((x) => {
          // if (row == 'fileList' && x.fieldName == 'wayFiles') {
          //   x.initialValue = this.editRow.fileList.map((f) => ({ name: f.title, url: f.fileUrl }));
          // }
          if(x.fieldName == 'destoryStatus'){
            x.initialValue = res.data.find(x => x.codeId == this.editRow.destoryStatus).codeCnDesc;
          }
          if (x.fieldName == row) {
            x.initialValue = this.editRow[row];
          }
        });
      }
      let res2 = await getPartType({type: 5500});
      this.columns.find(x => x.dataIndex == 'ifReturn').editItems = res2.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));

    },
    onCellChange(columns, row) {
      let sum = 0;
      this.$refs.table.$refs.pageTable.list.map((x) => {
        console.log(x);
        sum += Number(x.pestructionCount);
      });
      this.formList.find((x) => x.fieldName == 'destoryCount').initialValue = sum;
      this.formList.find((x) => x.fieldName == 'cmsClaimCount').initialValue = this.$refs.table.$refs.pageTable.list.length;
    },
    add() {
      this.visibleList = true;
    },
    closeAdd() {
      this.visibleList = false;
    },
    saveAdd(val) {
      this.visibleList = false;
      let list = Object.assign([], val);
      let pageList = this.$refs.table.$refs.pageTable.list;
      val.map((x) => {
        if (pageList.find((f) => f.claimId != x.claimId)) {
          list.push(x);
        }
      });
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE(this.$refs.table.$refs.pageTable.list);
      this.$refs.table.$refs.pageTable.EXECUTE_INSERT(list);
      this.formList.find((x) => x.fieldName == 'cmsClaimCount').initialValue = this.$refs.table.$refs.pageTable.list.length;
      let num = 0;
      this.$refs.table.$refs.pageTable.list.map((x) => {
        num += Number(x.pestructionCount || 0);
      });
      this.formList.find((x) => x.fieldName == 'destoryCount').initialValue = num;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandleFile(val) {
      this.saveDataFile = val;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    deleteHandle(row) {
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE([row]);
    },
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    chooseOrderNo() {
      this.visible = true;
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '销毁单号',
          fieldName: 'destoryNo',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '单据状态',
          fieldName: 'destoryStatus',
          placeholder: '',
          readonly: true,
          initialValue: '',
          disabled: true, // 置灰
        },
        {
          type: 'INPUT',
          label: '索赔单数量',
          fieldName: 'cmsClaimCount',
          placeholder: '',
          readonly: true,
          initialValue: '',
          disabled: true, // 置灰
        },
        {
          type: 'INPUT',
          label: '销毁数量',
          fieldName: 'destoryCount',
          placeholder: '',
          readonly: true,
          disabled: true, // 置灰
          initialValue: '',
        },
        {
          type: 'DATE',
          label: '预计销毁日期',
          fieldName: 'destoryDate',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          placeholder: '请选择日期...',
          disabled: true, // 置灰
          rules: [{ required: true, message: '请选择日期', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '操作人',
          fieldName: 'destoryBy',
          placeholder: '请输入操作人...',
          disabled: true, // 置灰
          rules: [{ required: true, message: '请输入操作人', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '操作人电话',
          fieldName: 'destoryTel',
          placeholder: '请输入操作人电话...',
          disabled: true, // 置灰
          rules: [
            { required: true, message: '请输入操作人电话', trigger: 'blur' },
            {
              min: 11,
              max: 11,
              message: '长度需要是11个字符',
              trigger: 'blur',
            },
          ],
          initialValue: '',
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remarks',
          placeholder: '备注',
          disabled: true, // 置灰
          rows: 3,
          selfCols: 4, // 元素占据几行
          initialValue: '',
        },
        // {
        //   type: 'UPLOAD_FILE',
        //   label: '上传附件',
        //   fieldName: 'wayFiles',
        //   placeholder: '上传附件...',
        //   disabled: true, // 置灰
        //   rules: [{ required: true, message: '请上传文件', trigger: 'change' }],
        //   initialValue: [],
        //   upload: {
        //     actionUrl: '/api/file/oss/upload',
        //     tipText: '支持PDF、RAR、JPG格式',
        //   },
        // },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '附件标题',
          dataIndex: 'title',
          showOverflowTooltip: true,
          render: p => {
            return (
              <a style="color:#bb0a30" href={p.row.fileUrl} target="_blank" download={p.row.title}>
                {p.row.title}
              </a>
            );
          }
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '销毁数量', dataIndex: 'pestructionCount', sorter: true },
        { title: '销毁说明', dataIndex: 'partRemarks', sorter: true },
        { title: '是否回运', dataIndex: 'ifReturn', sorter: true , editType: 'select', editItems: [] }
      ];
    },
    creatHeader1() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '操作人', dataIndex: 'createdByStr', sorter: true },
        { title: '审核时间', dataIndex: 'checkAt', sorter: true },
        { title: '审核状态', dataIndex: 'statusStr', sorter: true },
        { title: '审核说明', dataIndex: 'checkRemarks', sorter: true },
      ];
    },
  },
};
</script>

<style lang="less" scoped></style>
